package capricornus.item
{
	import capricornus.components.sprite.AbstractComponent;
	import capricornus.events.ResLoaderEvent;
	import capricornus.item.attribute.ItemAttribute;
	import capricornus.res.Image;
	import capricornus.res.ImageFitType;
	
	[Event(name="itemShapeChange", type="capricornus.item.ItemEvent")]
	public class Item extends AbstractComponent
	{
		protected var _parentContainer:ItemContainer;
		protected var _attribute:ItemAttribute;
		
		private var _image:Image;
		
		public function Item( att:ItemAttribute=null )
		{
			super();
			_attribute = att;
			init();
		}
		
		protected function init():void
		{
			if( _attribute==null )
			{
				createItemAttribute();
			}
			_image = new Image( ImageFitType.EXACT_FIT );
			_image.addEventListener( ResLoaderEvent.COMPLETE, onImageLoaded, false, 0, true );
			
			addChild( _image );
		}
		
		public function destroy():void
		{
			_attribute = null;
			
			if( _image != null )
			{
				_image.removeEventListener( ResLoaderEvent.COMPLETE, onImageLoaded );
				_image.destroy();
				_image = null;
			}
			_parentContainer = null;
		}
		
		public function get attribute():ItemAttribute
		{
			return _attribute;
		}
		
		override public function setSize(w:int, h:int):void
		{
			super.setSize( w, h );
			
			if( _image != null )
			{
				_image.setSize( w, h );
				_image.source = "E:\\oo.jpg";
			}
		}
		
		internal function setParentContainer( value:ItemContainer ):void
		{
			_parentContainer = value;
		}
		
		public function get parentContainer():ItemContainer
		{
			return _parentContainer
		}
		
		protected function createItemAttribute():void
		{
			_attribute = new ItemAttribute();
		}
		
		private function onImageLoaded( e:ResLoaderEvent ):void
		{
			dispatchEvent( new ItemEvent( ItemEvent.ITEM_SHAPE_CHANGE ) );
		}
	}
}